package org.person.gift.strategy;


import org.person.gift.exception.ResponseCode;
import org.person.gift.exception.SystemAssert;

/**
 * 这是用来创建 分组策略对象的工厂
 */
public class ShopCarDataSyncKeyGroupStrategyFactory {

    /*public IShopCarDataSyncKeyGroupStrategy getStrategy(String groupType){
        if("hash".equals(groupType)){
            return new ShopCarDataSyncKeyHashGroupStrategy();
        }else if("order".equals(groupType)){
            return new ShopCarDataSyncKeyOrderGroupStrategy();
        }else{
            throw new RuntimeException("不支持该分组策略!");
        }
    }*/

    /**
     * 传一个全限定名过来,通过反射创建对象
     * @param clazz
     * @return
     */
    public static IShopCarDataSyncKeyGroupStrategy getStrategy(String clazz){
        try {
            SystemAssert.isNotBlank(clazz, ResponseCode.RESPONSE_CODE_500);

            Class<?> aClass = Class.forName(clazz);
            Object obj = aClass.newInstance();
            if(obj instanceof IShopCarDataSyncKeyGroupStrategy){
                return (IShopCarDataSyncKeyGroupStrategy)obj;
            }
            throw new RuntimeException("不支持该分组策略!");
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
    }
}
